﻿@using Microsoft.AspNetCore.Components.Web

@foreach (var button in Buttons)
{
    <button @key="button.Id" @onclick="button.Action"> @* Compliant *@
        Button #@button.Id
    </button>
}

@foreach (var button in Buttons)
{
    <button @key="button.Id" @onclick="(e) => button.Action(e)"> @* Noncompliant *@
        Button #@button.Id
    </button>
}

@for (int i = 0; i < Buttons.Count; i++)
{
    var buttonNumber = i;
    <button @onclick="@(e => DoAction(e, Buttons[buttonNumber]))"> @* Noncompliant *@
        Button #@buttonNumber
    </button>
}

@{
    var j = 0;
    while (j < 5)
    {
        j += 1;
        <button @onclick="(e) => Reset(e)"> @* Noncompliant *@
            Reset @j
        </button>
    }

    do
    {
        j += 1;
        <button @onclick="(e) => Reset(e)"> @* Noncompliant *@
            Reset @j
        </button>
    } while (j < 10);
}

<button @onclick="(e) => Reset(e)">Reset</button> @* Compliant *@

@if (Buttons.Count > 0)
{
    <button @onclick="(e) => Reset(e)">Reset #2</button> @* Compliant *@
}

@foreach (var button in Buttons.OrderByDescending(x => x.Id)) @* Compliant, the lambda is executed outside of the loop *@
{
    <p>@button.Id</p>
}

@code {
    private List<Button> Buttons { get; } = new List<Button>();

    private void DoAction(MouseEventArgs e, Button button) { }

    private class Button
    {
        public string Id { get; } = Guid.NewGuid().ToString();
        public Action<MouseEventArgs> Action { get; set; } = e => { };
    }

    private void Reset(MouseEventArgs mouseEventArgs) 
    { 
        foreach (var button in Buttons)
        {
            button.Action = e => { }; // Compliant
        }
    }
}
